package com.cjx.stars.interceptor;

import cn.hutool.json.JSONUtil;
import com.cjx.stars.common.Response;
import com.cjx.stars.common.ResponseUtils;
import com.cjx.stars.constant.FailMessage;
import com.cjx.stars.utils.UserHolder;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1.判断是否需要拦截（ThreadLocal中是否有用户）
        //todo 打开拦截器
        /*if (UserHolder.getUser() == null) {
            System.out.println("已拦截");
            // 创建 Response 对象
            Response fail = ResponseUtils.fail(FailMessage.ERROR_THE_USER_IS_NOT_LOGGED_IN);
            String jsonStr = JSONUtil.toJsonStr(fail);
            // 设置响应内容类型和字符编码
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            /// 将 JSON 字符串写入响应体
            response.getWriter().write(jsonStr);
            // 拦截
            return false;
        }*/
        // 有用户，则放行
        return true;
    }


}